package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.ArrayToListUtil;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public List<Company> findAll() {
        List<Company> all = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            all = mapper.findAll();
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return all;
    }

    @Override
    public Company findById(String id) {
        SqlSession sqlSession = null;
        Company company = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            company = mapper.findById(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

        return company;
    }

    @Override
    public void delete(String...id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            mapper.delete(ArrayToListUtil.toList(id));
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            company.setId(UUID.randomUUID().toString());
            company.setState(1);
            mapper.save(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    @Override
    public void update(Company company) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            mapper.update(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }


    }

    @Override
    public PageInfo findAll(Integer pageNum, Integer pageSize) {
        PageInfo<Company> companyPageInfo = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<Company> all = mapper.findAll();
            companyPageInfo = new PageInfo<>(all);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return companyPageInfo;
    }
}
